package animator.phantom.exec;

/*
    Copyright Janne Liljeblad

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.*;

//--- Executes series of native commands. Thay can be completed or timed out.
public class NativeCommandRunner
{
	private LinkedList commands = new LinkedList();
	private Vector<NativeCommandExec> unfinishedCommands = new Vector();
	private NativeCommandExec currentCommand = null;

	//--- add command
	public void addCommand( NativeCommandExec command )
	{
		command.setRunner( this );
		commands.add( command );
	}

	//--- Start running commands.
	public void runCommands()
	{
		if( commands.size() == 0 ) return;
		NativeCommandExec c = ( NativeCommandExec ) commands.pop();
		currentCommand = c;
		c.runCommand();
	}

	//--- Previous complete. maybe try next.
	public void complete( NativeCommandExec last, boolean wasCompleted )
	{
		if( !wasCompleted ) unfinishedCommands.add( last );
		if( commands.size() == 0 ) return;//all done
		//--- next
		NativeCommandExec next = ( NativeCommandExec ) commands.pop();
		next.runCommand();
	}
	
	public void stopCommand( boolean stopRunner )
	{
		currentCommand.stop( stopRunner );
	}

}//end class